/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import listeners.PopupListener;
import diagram.locations.AbsoluteLocation;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;
import javax.swing.TransferHandler;
import listeners.DragMouseAdapter;
import model.ToolbarModel;

/**
 *
 * @author Jelle
 */
public class Toolbar extends JToolBar implements MouseListener, Observer {

    private ToolbarPopup popup;
    private ToolbarModel tmodel;

    Toolbar(ToolbarModel tmodel) {
        this.addMouseListener(this);
        super.setOrientation(1);
        this.tmodel = tmodel;
        MouseListener listener = new DragMouseAdapter();

        ImageIcon select = new ImageIcon("icons\\node.png");
        select.setDescription("node");
        ImageIcon select2 = new ImageIcon("icons\\trigger.png");
        select2.setDescription("handle");
        ImageIcon select3 = new ImageIcon("icons\\connector.png");
        select3.setDescription("connect");

        JButton selecta = new JButton(select);
        selecta.addMouseListener(listener);
        selecta.setTransferHandler(new TransferHandler("icon"));

        JButton selectb = new JButton(select2);
        selectb.addMouseListener(listener);
        selectb.setTransferHandler(new TransferHandler("icon"));

        JButton selectc = new JButton(select3);
        selectc.addMouseListener(listener);
        selectc.setTransferHandler(new TransferHandler("icon"));

        popup = new ToolbarPopup(tmodel);
       // PopupListener pl = new PopupListener(popup, tmodel);
        //this.addMouseListener(pl);

        tmodel.addItem(selecta);
        add(selecta);
        tmodel.addItem(selectb);
        add(selectb);    
        tmodel.addItem(selectc);
        add(selectc);
    }

    public void mouseClicked(MouseEvent e) {
        System.out.println(e.getComponent().getClass().getName());
    }

    public void mousePressed(MouseEvent e) {
        // popup.menuToolbarItem(new AbsoluteLocation(e.getX(), e.getY()), null);
    }

    public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger()) {
            System.out.println(e.getComponent().getClass().getName());
        }

        popup.menuToolbar(new AbsoluteLocation(e.getX(), e.getY()), tmodel);


    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void update(Observable o, Object arg) {
        System.out.println("AHOI");
        removeAll();
        System.out.println("sss" + tmodel.getItems().size());
        for (JButton b : tmodel.getItems()) {
            add(b);
        }
        repaint();
    }
}
